package com.cy.util;


//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//import java.security.spec.EncodedKeySpec;

public class Util {
    public static String EncoderByMd5(String str) {
        //确定算法
//        MessageDigest md5 = MessageDigest.getInstance("MD5");
//        BASE64Encoder base64en = new BASE64Encoder();
//        //加密后的字符串
//        String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
//        return newstr;
        try {
//            dataStr = dataStr ;
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(str.getBytes("UTF8"));
            byte s[] = m.digest();
            String result = "";
            for (int i = 0; i < s.length; i++) {
                result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
//        System.out.println(EncoderByMd5("admin"));
//    }
    public static boolean getPrice(String str) {
        String regex = "^[0-9]*$";
        boolean isfalg = str.matches(regex);
        if (isfalg) {
            return true;
        }
        return false;
    }

    //获取当前时间
    public static String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return formatter.format(date);
    }
    public static String getData() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        return formatter.format(date);
    }

    //base64加密
    public static String base64(String str) {
//        BASE64Encoder encoder = new BASE64Encoder();
//        String encode = encoder.encode(str.getBytes());//加密,为String
//        return encode;
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
    }
    //base64解密
    public static String solveBase64(String str) {
//        BASE64Decoder decoder = new BASE64Decoder();
//        return new String(decoder.decodeBuffer(str));//解密.解密为数组
        Base64.Decoder decoder = Base64.getDecoder();
        return new String(decoder.decode(str), StandardCharsets.UTF_8);
    }

    public static boolean isInteger(String input) {

        Matcher mer = Pattern.compile("^[+]?[0-9]+$").matcher(input);

        return mer.find();

    }

    public static boolean isDouble(String input) {

        Matcher mer = Pattern.compile("^[+]?[0-9.]+$").matcher(input);

        return mer.find();

    }

    //银行卡号
    public static boolean getBanCard(String str) {
        String regex = "^([1-9]{1})(\\d{15}|\\d{16}|\\d{18})$";
        boolean isfalg = str.matches(regex);
        if (isfalg) {
            return true;
        }
        return false;
    }

    //验证码生成
    public static String validation() {
        String strAry = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        String str = "";
        for (int i = 0; i < 4; i++) {
            str += strAry.charAt(new Random().nextInt(strAry.length()));
        }
        return str;
    }
    //生成链接
    public static SqlSession getSqlSer(){
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
    //关闭链接
    public static void relSqlSer(SqlSession sqlSession){
        sqlSession.close();
    }
    public static String getRequestBody(HttpServletRequest request) {//读取req里的json字符串
        try {
            InputStream v_inputstream = request.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int x = 0;
            while ((x = v_inputstream.read()) != -1) {
                baos.write(x);
            }
            baos.flush();
            return new String(baos.toByteArray(),"UTF-8");
        } catch (Exception e) {
            return "";
        }
    }
    public static String getType(Object object){//输出对象类型
        String typeName=object.getClass().getName();
        int length= typeName.lastIndexOf(".");
        String type =typeName.substring(length+1);
        return type;
    }
    //获得星期几
    public static String getWeek(String sdate) throws ParseException {
        // 再转换为时间
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Date parseDate = dateFormat.parse(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(parseDate);
        int week_index = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }
    public static void main(String[] args) {
        System.out.println(new String[]{"1,2,3"}[0]);
    }
}
